package edu.zcmu.seckill.entity;

import com.alibaba.fastjson.annotation.JSONField;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;

import edu.zcmu.seckill.enumeration.OrderStatusEnum;
import edu.zcmu.seckill.utils.SnowFlake;
import lombok.Getter;
import lombok.Setter;

/**
 * <p>
 * 订单表
 * </p>
 *
 * @author 唐诗
 * @since 2022-02-22
 */
@Getter
@Setter
@TableName("product_order")
public class Order implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 订单主键
     */
    @TableId("id")
    private String id;

    /**
     * 产品id
     */
    @TableField("product_id")
    private Integer productId;

    /**
     * 产品名称
     */
    @TableField("product_name")
    private String productName;

    /**
     * 用户id
     */
    @TableField("user_id")
    private Integer userId;

    /**
     * 支付金额
     */
    @TableField("total_price")
    private BigDecimal totalPrice;

    /**
     * 订单状态/0 待支付/1 已取消/2 已超时/3金额异常
     */
    @TableField("order_status")
    private Integer orderStatus;

    /**
     * 订单创建时间
     */
    @TableField("create_time")
    @JSONField(format = "yyyy-MM-dd HH:mm:ss")
    private Date createTime;



    public static Order createOrder(Product product,String uid){
        Order order = new Order();
        order.setUserId(Integer.valueOf(uid));
        order.setProductId(product.getId());
        order.setProductName(product.getName());
        order.setTotalPrice(product.getPrice());
        order.setCreateTime(new Date());
        order.setOrderStatus(OrderStatusEnum.ORDER_WAITING.getCode());
        order.setId(new SnowFlake(2, 3).nextId());
        return order;
    }

}
